<?php
/*
 *  This file is part of Urd.
 *
 *  Urd is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *  Urd is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. See the file "COPYING". If it does not
 *  exist, see <http://www.gnu.org/licenses/>.
 *
 * $LastChangedDate: 2008-07-16 23:53:30 +0200 (Wed, 16 Jul 2008) $
 * $Rev: 1301 $
 * $Author: gavinspearhead $
 * $Id: action.php 1301 2008-07-16 21:53:30Z gavinspearhead $
 */
define('ORIGINAL_PAGE', $_SERVER['PHP_SELF']);

$pathac = realpath(dirname(__FILE__));
require_once "$pathac/../functions/ajax_includes.php";
require_once "$pathac/../functions/buttons.php";

$prefs = load_config($db);
$uc = new urdd_client($prefs['urdd_host'], $prefs['urdd_port'], $userID);

if ($uc->is_connected() === FALSE) 
	throw new exception($LN['error_urddconnect']);

if (isset($_GET['cmd']) && $_GET['cmd'] == 'export_all')
    $command = 'export_all';
else if (!isset($_POST['cmd'])) 
    die_html ('Command not found');
else {
    $command = strtolower($_POST['cmd']);
    verify_challenge($_POST['challenge']);
}

$id = isset($_REQUEST['id'])?$_REQUEST['id']:'';

switch ($command) {
case 'findservers':
    if (!$isadmin)
        fatal_error($LN['error_noadmin']);
    $uc->findservers();
	$uc->disconnect();
	die_html('OK');
	break;
case 'findservers_ext':
    if (!$isadmin)
        fatal_error($LN['error_noadmin']);
    $uc->findservers('extended');
	$uc->disconnect();
	die_html('OK');
	break;
case 'cleandb_all':
    if (!$isadmin)
        fatal_error($LN['error_noadmin']);
	$uc->cleandb('all');
	$uc->disconnect();
	die_html('OK');
	break;
case 'cleandb':
    if (!$isadmin)
        fatal_error($LN['error_noadmin']);
	$uc->cleandb('now');
	$uc->disconnect();
	die_html('OK');
	break;
case 'updatearticles':
    if (!$isadmin)
        fatal_error($LN['error_noadmin']);
	$uc->update('all');
	$uc->disconnect();
	add_stat_data($db, STAT_ACTION_UPDATE, 'all', $userID);
	die_html('OK');
	break;
case 'updategroups':
    if (!$isadmin)
        fatal_error($LN['error_noadmin']);
	$uc->update_newsgroups();
	$uc->disconnect();
	die_html('OK');
	break;
case 'gensetsarticles':
    if (!$isadmin)
        fatal_error($LN['error_noadmin']);
	$uc->gensets('all');
	$uc->disconnect();
	die_html('OK');
	break;
case 'purgearticles':
    if (!$isadmin)
        fatal_error($LN['error_noadmin']);
	$uc->purge('all');
	$uc->disconnect();
	add_stat_data($db, STAT_ACTION_EXPIRE, 'all', $userID);
	die_html('OK');
	break;
case 'expirearticles':
    if (!$isadmin)
        fatal_error($LN['error_noadmin']);
	$uc->expire('all');
	$uc->disconnect();
	add_stat_data($db, STAT_ACTION_EXPIRE, 'all', $userID);
	die_html('OK');
	break;
case 'gensetsgroup':
    if (!$isadmin)
        fatal_error($LN['error_noadmin']);
	$id = $_REQUEST['group'];
	$uc->gensets($id);
	$uc->disconnect();
	die_html('OK');
	break;
case 'updategroup':
	$id = $_REQUEST['group'];
	$uc->update($id);
	$uc->disconnect();
	add_stat_data($db, STAT_ACTION_UPDATE, $id, $userID);
	die_html('OK');
	break;
case 'updaterssall':
    if (!$isadmin)
        fatal_error($LN['error_noadmin']);
	$uc->update_rss('all');
	$uc->disconnect();
	add_stat_data($db, STAT_ACTION_UPDATE, 'all', $userID);
	die_html('OK');
	break;
case 'updaterss':
	$id = $_REQUEST['group'];
	$uc->update_rss($id);
	$uc->disconnect();
	add_stat_data($db, STAT_ACTION_UPDATE, $id, $userID);
	die_html('OK');
	break;
case 'optimise':
    if (!$isadmin)
        fatal_error($LN['error_noadmin']);
	$uc->optimise();
	$uc->disconnect();
	die_html('OK');
	break;
case 'checkversion':
	$uc->check_version();
	$uc->disconnect();
	die_html('OK');
	break;
case 'expiregroup':
	$id = $_REQUEST['group'];
	$db->escape($id);
	$uc->expire($id);
	$uc->disconnect();
	add_stat_data($db, STAT_ACTION_EXPIRE, $id, $userID);
	die_html('OK');
	break;
case 'expirerssall':
    if (!$isadmin)
        fatal_error($LN['error_noadmin']);
	$uc->expire_rss('all');
	$uc->disconnect();
	add_stat_data($db, STAT_ACTION_EXPIRE, 'all', $userID);
	die_html('OK');
	break;
case 'purgerssall':
    if (!$isadmin)
        fatal_error($LN['error_noadmin']);
	$uc->purge_rss('all');
	$uc->disconnect();
	add_stat_data($db, STAT_ACTION_PURGE, 'all', $userID);
	die_html('OK');
	break;
case 'expirerss':
	$id = $_REQUEST['group'];
	$db->escape($id);
	$uc->expire_rss($id);
	$uc->disconnect();
	add_stat_data($db, STAT_ACTION_EXPIRE, $id, $userID);
	die_html('OK');
	break;
case 'purgerss':
	$id = $_REQUEST['group'];
	$db->escape($id);
	$uc->purge_rss($id);
	$uc->disconnect();
	add_stat_data($db, STAT_ACTION_PURGE, $id, $userID);
	die_html('OK');
	break;
case 'purgegroup':
	$id = $_REQUEST['group'];
	$db->escape($id);
	$uc->purge($id);
	$uc->disconnect();
	add_stat_data($db, STAT_ACTION_PURGE, $id, $userID);
	die_html('OK');
	break;
case 'getsetinfo':
    if (!$isadmin)
        fatal_error($LN['error_noadmin']);
	$uc->getsetinfo();
	$uc->disconnect();
	die_html('OK');
	break;
case 'sendsetinfo':
    if (!$isadmin)
        fatal_error($LN['error_noadmin']);
	$uc->sendsetinfo();
	$uc->disconnect();
	die_html('OK');
	break;
case 'cleandir':
    if (!$isadmin)
        fatal_error($LN['error_noadmin']);
	$uc->cleandir ('all');
	$uc->disconnect();
	die_html('OK');
	break;
case 'unschedule': 
	$job = $_POST['job'];
	$uc->unschedule($job, '');
	$uc->disconnect();
	die_html('OK');
case 'cancel': 
	$task = $_POST['task'];
	$uc->cancel($task);
	$uc->disconnect();
	die_html('OK');
case 'pause': 
	$task = $_POST['task'];
	$uc->pause($task);
	$uc->disconnect();
	die_html('OK');
case 'continue': 
	$task = $_POST['task'];
	$uc->continue_cmd($task);
	$uc->disconnect();
    die_html('OK');
case 'export_all':
    if (!$isadmin)
        fatal_error($LN['error_noadmin']);
    header("Content-Type: text/html/force-download");
	header("Content-Disposition: attachment; filename=urd_all_settings.xml");
    $xml = new urd_xml_writer('php://output');
    $xml->write_all($db);
    $xml->output_xml_data();
	die;
case 'import_all':
    
    if (!$isadmin)
        fatal_error($LN['error_noadmin']);
    if (isset($_FILES['filename']['tmp_name'])) {
        try {
            $xml = new urd_xml_reader($_FILES['filename']['tmp_name']);
            $settings = $xml->read_all_settings($db);
            clear_all_feeds($db, $userID);
            clear_all_groups($db, $userID);
            clear_all_usenet_servers($db);
            clear_all_buttons($db);
            stop_urdd();
            clear_all_users($db);
            clean_config($db);

            reset_config($db);
            set_configs($db, $settings['config']);
            set_all_users($db, $settings['users'], $settings['users_settings']);
            set_all_buttons($db, $settings['buttons']);
            set_all_usenet_servers($db, $settings['usenet_servers']);
            $userID = get_admin_userid($db); // we need to get the ID of the first admin we find since we reloaded users
            start_urdd();
            $s_time = time();
            $uprefs = load_config($db);
            while(1) {
                try {
                    $uc = new urdd_client($uprefs['urdd_host'], $uprefs['urdd_port'], $userID);
                } catch (execption $e) {}
                    if ($uc->is_connected()) break;
                    else { 
                        sleep (1);// sleep one second before we try again
                        if (time() - $s_time >= 60) // until 1 minute has passed... probably an incompatible config loaded
                            throw new exception ('urdd hasn\'t started yet... stopping');
                    }
            }
            set_all_groups($db, $settings['newsgroups'], $userID);
            set_all_feeds($db, $settings['rssfeeds'], $userID);
        } catch (exception $e) {
            die_html("Error occurerd {$e->getMessage()}");
        }
        redirect('index.php');
    }
    
    die;

default:
	die_html ($LN['error_novalidaction']);
	break;
}


?>
